C Primier Plus --chapter three

Namespace

std::cin use the name from the namespace std.
using namespacename::name;
after the using declaration we can use the name directly.
A Separate using Declaration Is Required for Each Name

Each using declaration introduces a single namespace member


initialization of string

1
2
3
4
string s1; // default initialization; s1 is the empty string
string s2 = s1; // s2 is a copy of s1
string s3 = "hiya"; // s3 is a copy of the string literal
string s4(10, 'c'); // s4 is cccccccccc

os<< s Write s onto output stream os.return os.
is>> s Reads whitespace-seperated string from is into s.return is.
getline(is,s) Reads a line of input from is to s.return is.
s.empty() Returns true if s is empty; otherwise returns false.
s.size() Returns the number of characters in s.
s[n] Returns a reference to the char at position n in s;position start at 0.
s1+s2 Returns a string that is concatenation of s1 and s2.
!= ,<,<=,>,>= Comparisons are case-sensitive and use dictionary.

1
2
3
4
The string input operator reads and discards any leading whitespace
(e.g., spaces, newlines, tabs). It then readscharacters until the next
whitespace character is encountered
Reading an Unknown Number of strings

getline() reads to (including)the first newline,but not store in the string.
empty:member function of string``return bool

size()
return string::size_type

always use auto or decltype to let the compiler to provide the approriate

1
2
DEV-C++支持C++11标准:
Tools>-Compiler Option >-Code Generation >- Language standart

the range for statement

1
2
for (auto c : str) // for every char in str
cout << c << endl; // print the current character followed by a newline
1
2
3
for (auto &c : s) // for every char in s (note: c is a reference)
c = toupper(c); // c is a reference, so the assignment changes the char in s
cout << s << endl;

two ways to access individual characters in a string
subscript or iterator

Any time we use a subscript, we must ensure that there is a value at the given location

vector

:contaner class template
Vector is a template, not a type. type generated from vector must include the element type.for example vector
Vector initialize

1
2
3
4
5
6
7
8
9
10
vector<int> ivec; // initially empty // give ivec some values
vector<int> ivec2(ivec); // copy elements of ivec into ivec2
vector<string> v1{"a", "an", "the"}; // list initialization
vector<int> v1(10); // v1 has ten elements with value 0
vector<int> v2{10}; // v2 has one element with value 10
vector<int> v3(10, 1); // v3 has ten elements with value 1
vector<int> v1(10); // v1 has ten elements with value 0
vector<int> v2{10}; // v2 has one element with value 10
vector<int> v3(10, 1); // v3 has ten elements with value 1
vector<int> v4{10, 1}; // v4 has two elements with values 10 and 1

The subscript operator on vector (and string) fetches an existing element; it does not add an element.


Iterators

:indirect access to an object
the iterator returned from end does not denote an element

1
2
auto it3 = v.cbegin(); // it3 has type vector<int>::const_iterator
return a const_iterator ,only read bur not write to an object.

The arrow operator combines dereference and member access into a single operation. That is, it->mem is a synonym for (\ it).mem*.
Iterators for string and vector support additional operations that can move an iterator multiple elements at a time and all the relational operators

Arrays

elements in an array default initialized

1
2
char a3[] = "C++"; // null terminator added automatically
const char a4[6] = "Daniel"; // error: no space for the null!

Array can`t be intialized by copy and assignment.
operations on arrays are often really operations on pointers such as auto.
but when we use decltype, The type returned by decltype(ia) is array of ten ints:

1
2
3
int ia[] = {0,1,2,3,4,5,6,7,8,9}; // ia is an array of ten ints
int *beg = begin(ia); // pointer to the first element in ia
int *last = end(ia); // pointer one past the last element in ia

Unlike subscripts for vector and string, the index of the built-in subscript operator is not an unsigned type
c styled string function such as strlen() strcmp() strcat() strcpy() are not verify to string parameter.
c_str() indicates that the function returns a C-style character string
it returns a pointer to the beginning of a null-terminated character array that holds the same data as the characters in the string
The parentheses in this declaration are essential

1
2
int *ip[4]; // array of pointers to int
int (*ip)[4]; // pointer to an array of four ints